<?php
	namespace Tools;
	use \Think\Controller;
//公用控制器
	class PublicController extends Controller{
		//检测用户是否登录系统，已登录返回个人信息，未登录则跳转至登录页面	
		function require_login(){
			if(empty(session())){
				$this->redirect('User/integrallogin');
			}else{
				$my=D('user')->where("id=".session('id'))->find();
				return $my;
			}
		}
        //检测用户是否登录系统，已登录返回个人信息，未登录则跳转至登录页面
        function require_wx_login(){
            if(empty(session('uid'))){
                $this->redirect('Draw/wxcilentlogin');
            }else{
                $my=D('user')->where("uid=".session('uid'))->find();
                return $my;
            }
        }
		//发送下单成功微信模板消息
		public static function send($token){
			$msg_info=D('shop_msg')->where('uid=1')->find();
			$touser=$msg_info['touser'];
			$tmsg=array(
	                    "touser"=>$touser,
	                    "template_id"=>"OgcOQBknQcavmQCHtduk4eICIeKyArwoZvilj8dAUcM",
	                    "url"=>"http://www.zby521.com/?_a=shop&_u=sp.orderlist&key=".$order['uid'],
	                    "data"=>array(
							"first"=>array("value"=>"收到一条新订单，请尽快处理！","color"=>"#173177"),
						)
	                 );	
	  		$url='https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token;
	        $ret = self::weixin_https_post($url, $tmsg);
			return $ret;
	}
/*
		调用微信服务器接口 https post
	*/
public static function weixin_https_post($url, $data)
	{

		if (is_array($data))
		{
			$data = json_encode($data, JSON_UNESCAPED_UNICODE);
		}
		$c = curl_init();
		curl_setopt($c, CURLOPT_URL, $url);
		curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($c, CURLOPT_POST, true);
		curl_setopt($c, CURLOPT_POSTFIELDS, $data);
		$ret = curl_exec($c);
		curl_close($c);

		return $ret;
	}
	public static function gettoken($appid="wx935cdb3a2a049e5c",$secret="083e078b99a9b22e26d7d223ecd90c34"){
		$msg_info=D('shop_msg')->where('uid=1')->find();
		$appid=$msg_info['appid'];
		$secret=$msg_info['appsecret'];	
		$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;	
		$html = file_get_contents($url);
		$res=json_decode($html,true); 
		return $res['access_token'];
//		$this->ajaxReturn($res['access_token']);
//		echo $res['access_token'];
//		$this->display();
	}	
		
	}
?>